/*     */ package com.newcrm.webapi.sub;
/*     */ 
/*     */ import com.newcrm.bean.CompanyInfo;
/*     */ import com.newcrm.bean.Group;
/*     */ import com.newcrm.bean.User;
/*     */ import com.newcrm.db.CustomerManager;
/*     */ import com.newcrm.exception.NotFoundException;
/*     */ import com.newcrm.util.Util;
/*     */ import com.newcrm.webapi.AbstractAction;
/*     */ import com.newcrm.webapi.AbstractHttpServlet;
/*     */ import java.io.IOException;
/*     */ import java.io.PrintWriter;
/*     */ import java.util.ArrayList;
/*     */ import java.util.Date;
/*     */ import java.util.HashMap;
/*     */ import java.util.List;
/*     */ import java.util.Map;
/*     */ import javax.servlet.ServletException;
/*     */ import javax.servlet.http.HttpServletRequest;
/*     */ import javax.servlet.http.HttpServletResponse;
/*     */ import net.sf.json.JSONObject;
/*     */ 
/*     */ public class ShowCustomerCategoryChangeChartData extends AbstractAction
/*     */ {
/*     */   public ShowCustomerCategoryChangeChartData(AbstractHttpServlet servlet)
/*     */   {
/*  39 */     super(servlet);
/*     */   }
/*     */ 
/*     */   public void parse(HttpServletRequest request, HttpServletResponse response)
/*     */     throws IOException, ServletException, NotFoundException
/*     */   {
/*  46 */     User user = super.getLoginUser(request);
/*  47 */     if ((!user.isAdmin) && (!user.isGroupAdmin)) {
/*  48 */       super.showErrorMessage(response, "只有管理员或者部门经理才有查看的权限");
/*  49 */       return;
/*     */     }
/*     */ 
/*  52 */     String userId = Util.getURLParameter(request.getParameter("userId"));
/*  53 */     Date startDate = Util.str2Date(request.getParameter("start"));
/*  54 */     Date endDate = Util.str2Date(request.getParameter("end"));
/*     */ 
/*  62 */     if (!user.isAdmin)
/*     */     {
/*  64 */       if ((userId == null) || (userId.trim().length() == 0) || 
/*  65 */         (userId.contains("*"))) {
/*  66 */         userId = "g_" + user.groupId;
/*     */       }
/*     */     }
/*     */ 
/*  70 */     CompanyInfo company = super.getCompany(request);
/*  71 */     List result = new ArrayList();
/*  72 */     CustomerManager manager = new CustomerManager();
/*     */ 
/*  74 */     if ((userId == null) || (userId.trim().length() == 0))
/*     */     {
/*  76 */       result = manager.getAllCustomerCategoryChangeNum(user.companyId, 
/*  77 */         startDate, endDate);
/*  78 */     } else if (userId.contains("u_")) {
/*  79 */       if (userId.indexOf("*") > 0) {
/*  80 */         userId = "";
/*     */       }
/*     */       else {
/*  83 */         userId = userId.substring(2);
/*  84 */         if (!user.isAdmin)
/*     */         {
/*  86 */           User viewUser = company.getUser(userId);
/*  87 */           if (viewUser == null) {
/*  88 */             super.showErrorMessage(response, "您查看的员工不存在");
/*  89 */             return;
/*     */           }
/*     */ 
/*  92 */           int userGroupId = viewUser.groupId;
/*  93 */           if (!company.isGroupAdmin(user, userGroupId)) {
/*  94 */             super.showErrorMessage(response, "您没有权限查看该员工的统计信息");
/*  95 */             return;
/*     */           }
/*     */         }
/*     */       }
/*  99 */       result = manager.getCustomerCategoryChangeNumByUserId(
/* 100 */         user.companyId, userId, startDate, endDate);
/* 101 */     } else if (userId.contains("g_"))
/*     */     {
/* 103 */       int groupId = Util.str2Int(userId.substring(2));
/* 104 */       if (groupId < 0) {
/* 105 */         super.showErrorMessage(response, "您查看的部门信息不存在");
/* 106 */         return;
/*     */       }
/*     */ 
/* 109 */       if ((!user.isAdmin) && 
/* 110 */         (!company.isGroupAdmin(user, groupId))) {
/* 111 */         super.showErrorMessage(response, "您没有权限查看该员工的统计信息");
/* 112 */         return;
/*     */       }
/*     */ 
/* 115 */       String groupIds = "0";
/* 116 */       if (groupId > 0) {
/* 117 */         Group group = company.getGroupById(groupId);
/* 118 */         if (group == null) {
/* 119 */           super.showErrorMessage(response, "您查看的部门信息不存在");
/* 120 */           return;
/*     */         }
/*     */ 
/* 123 */         groupIds = group.getAllIds();
/*     */       }
/* 125 */       result = manager.getCustomerCategoryChangeNumByGroupIds(
/* 126 */         user.companyId, groupIds, startDate, endDate);
/*     */     }
/*     */ 
/* 129 */     response.setContentType("text/html;charset=utf-8");
/* 130 */     PrintWriter out = response.getWriter();
/* 131 */     JSONObject jsonObject = new JSONObject();
/*     */ 
/* 133 */     List chars = new ArrayList();
/* 134 */     List colors = new ArrayList();
/*     */ 
/* 136 */     int noneCategoryNum = 0;
/* 137 */     for (Object itemO : result) {
                Map item = (Map)itemO;
/* 138 */       int num = Util.str2Int(item.get("totalNum"));
/* 139 */       int categoryId = Util.str2Int(item.get("CategoryId"));
/* 140 */       String categoryName = company.getCustomerCategoryName(categoryId);
/* 141 */       if (categoryName != null) {
/* 142 */         Map map = new HashMap();
/* 143 */         map.put("label", categoryName);
/* 144 */         map.put("value", Integer.valueOf(num));
/* 145 */         map.put("id", Integer.valueOf(categoryId));
/* 146 */         chars.add(map);
/*     */ 
/* 148 */         String color = company.getCustomerCategoryColor(categoryId);
/* 149 */         if ((color == null) || (color.trim().length() == 0)) {
/* 150 */           color = "000000";
/*     */         }
/* 152 */         colors.add("#" + color);
/*     */       }
/*     */       else {
/* 155 */         noneCategoryNum += num;
/*     */       }
/*     */ 
/*     */     }
/*     */ 
/* 160 */     if (noneCategoryNum > 0) {
/* 161 */       Map map = new HashMap();
/* 162 */       map.put("label", "无分类");
/* 163 */       map.put("value", Integer.valueOf(noneCategoryNum));
/* 164 */       map.put("id", Integer.valueOf(0));
/* 165 */       chars.add(map);
/*     */ 
/* 167 */       colors.add("#000000");
/*     */     }
/*     */ 
/* 170 */     jsonObject.put("error", "0");
/* 171 */     jsonObject.put("chars", chars);
/* 172 */     jsonObject.put("colors", colors);
/* 173 */     out.println(jsonObject);
/* 174 */     out.flush();
/* 175 */     out.close();
/*     */   }
/*     */ }

/* Location:           C:\E\referenceProject\CRM参考\qikebao\WEB-INF\lib\qikebao.jar
 * Qualified Name:     com.newcrm.webapi.sub.ShowCustomerCategoryChangeChartData
 * JD-Core Version:    0.6.1
 */